package tags;

import groovy.lang.Closure;

import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;

import play.Logger;
import play.templates.FastTags;
import play.templates.GroovyTemplate.ExecutableTemplate;
import play.templates.JavaExtensions;
import utils.convert.Convert;
import utils.date.DateUtils;
import component.Page;

public class MyTags extends FastTags {
	/**
	 * 分页标签
	 */
	public static void _page(Map<String, Object> params, Closure body,
			PrintWriter out, ExecutableTemplate template, int fromLine) {

		int currPage = (Integer) (params.get("currPage") == null ? 1 :params.get("currPage"));
		int pageSize = (Integer) (params.get("pageSize") == null ? 10 : params
				.get("pageSize"));
		int totalCount = (Integer) params.get("totalCount");
		String condition = null;
		if (body != null) {
			condition = JavaExtensions.toString(body);
			condition = StringUtils.trim(condition);//condition的值字符串的两端可能会包含空白字符，此处trim
		}

		Page page = new Page();

		page.currPage = currPage;
		page.pageSize = pageSize;
		page.totalCount = totalCount;
		if (condition != null) {
			page.conditions = condition;
		}

		String pageTag = page.getThemeNumber();
		out.println(pageTag);

	}

	/**
	 * 计算两日期之间相差天时分
	 * 
	 * @author yanghao 2015年7月2日 下午4:42:34
	 */
	public static void _calDate(Map<String, Object> params, Closure body,
			PrintWriter out, ExecutableTemplate template, int fromLine) {
		String endDate = params.get("endDate") + "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date end = null;
		try {
			end = sdf.parse(endDate);
		} catch (ParseException e) {
			end = new Date();
		}
		Map<String, Object> map = DateUtils.getDiffTime(end, new Date());
		StringBuffer sb = new StringBuffer();
		sb.append(map.get("day")).append("天").append(map.get("hour"))
				.append("小时").append(map.get("min")).append("分");
		// 计算两日期相差天数
		out.print(sb.toString());
	}
	public static void _calDate2(Map<String, Object> params, Closure body,
			PrintWriter out, ExecutableTemplate template, int fromLine) {
		String endDate = params.get("endDate") + "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date end = null;
		try {
			end = sdf.parse(endDate);
		} catch (ParseException e) {
			end = new Date();
		}
		Map<String, Object> map = DateUtils.getDiffTime(end, new Date());
		StringBuffer sb = new StringBuffer();
		sb.append(map.get("day")).append("天").append(map.get("hour"))
				.append("小时");
		// 计算两日期相差天数
		out.print(sb.toString());
	}
	public static void _calTwoDiffDate(Map<String, Object> params, Closure body,
			PrintWriter out, ExecutableTemplate template, int fromLine) {
		String startDate = params.get("startDate")+"";
		String endDate = params.get("endDate") + "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date end = null;
		Date start = null;
		try {
			start = sdf.parse(startDate);
			end = sdf.parse(endDate);
		} catch (ParseException e) {
			end = new Date();
			start = new Date();
		}
		Map<String, Object> map = DateUtils.getDiffTime(end, start);
		StringBuffer sb = new StringBuffer();
		sb.append(map.get("day")).append("天").append(map.get("hour"))
				.append("小时").append(map.get("min")).append("分");
		// 计算两日期相差天数
		out.print(sb.toString());
	}

	public static void _addMonth(Map<String, Object> params, Closure body,
			PrintWriter out, ExecutableTemplate template, int fromLine) {
		String endDate = params.get("endDate") + "";
		int num = Convert.convertToInt(params.get("num")+"",0);
		Date endTime =null;
		try {
			endTime = DateUtils.getCurrentDateTimeStr(endDate, "yyyy-MM-dd HH:mm:ss");
			Date date =DateUtils.getNextMonth(endTime, num);
			out.print(DateUtils.getCurrentDateTimeStr(date, "yyyy-MM-dd HH:mm:ss"));
		} catch (Exception e) {
			 Logger.info("字符串转时间类型出错");
		}
	}
	/**
	 * format时间戳
	 * @Title:        _formatUnicodeDate 
	 * @Description:  TODO(这里用一句话描述这个方法的作用) 
	 * @param:        @param params
	 * @param:        @param body
	 * @param:        @param out
	 * @param:        @param template
	 * @param:        @param fromLine    
	 * @return:       void    
	 * @throws 
	 * @author        caofan
	 * @Date          2015年11月20日 下午5:39:53
	 */
	public static void _formatMsecDate(Map<String, Object> params, Closure body,
			PrintWriter out, ExecutableTemplate template, int fromLine){
		long msec = Convert.convertToLong(params.get("msec") + "",0);
		Date date = new Date();
		date.setTime(msec);
		out.print(DateUtils.getCurrentDateTimeStr(date,"yyyy-MM-dd HH:mm:ss"));
	}
}
